package com.ibm.cloud.banking.account.service;

import com.ibm.cloud.banking.annotation.PBS;
import com.ibm.cloud.banking.facade.account.IAccountFacade;
import com.ibm.cloud.banking.facade.account.dto.AccountInfoDTO;
import com.ibm.cloud.banking.platform.context.service.RequestContextHandler;
import com.ibm.cloud.invoker.PbsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@PBS(servNo = "PBS100001")
@Slf4j
public class SavingCustAcctService implements PbsService<AccountInfoDTO, AccountInfoDTO> {

    @Autowired
    private IAccountFacade accountFacade;

    private final static String COMPENSATE_PARAMS_KEY = "saving-cust-acct-param-key";

    @Override
    public AccountInfoDTO doService(AccountInfoDTO accountInfoDTO) {
        RequestContextHandler.putCompensate(COMPENSATE_PARAMS_KEY,accountInfoDTO);
        return accountFacade.savingCustAcct(accountInfoDTO);
    }

    @Override
    public void compensate(AccountInfoDTO data) {
        data = (AccountInfoDTO)RequestContextHandler.getCompensate(COMPENSATE_PARAMS_KEY);
        accountFacade.deleteAccount(data.getAccountNo());
    }
}
